package cn.socialcredits.qqcrawler;

import java.awt.Robot;
import java.awt.RenderingHints.Key;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent; 
import java.awt.event.KeyEvent;

public class QGroupCrawler {

	public static int MOUSE_LEFT = InputEvent.BUTTON1_MASK;
	public static int MOUSE_RIGHT = InputEvent.BUTTON2_MASK;
	public static int MOUSE_MIDDLE = InputEvent.BUTTON3_MASK; 
	
	private static Robot robot;
	static {
		try{
			robot = new Robot();
		} catch (Exception e) { 
		}
	}	
	public QGroupCrawler(){
		System.out.println("init done.");
	}
	public static void sleep(int time){
		try{
			Thread.sleep(time + 300);
	    } catch (Exception e) { 
		}
	}
	public void move(int x, int y){
		robot.mouseMove(x, y);
	}
	public void click(int button){
		robot.mousePress(button);
		robot.mouseRelease(button);
		sleep(100);
	}
	public void pressKey(int keycode){
		robot.keyPress(keycode);
		robot.keyRelease(keycode);
		sleep(500); 
	} 
	public void clear(){ 
		click(QGroupCrawler.MOUSE_LEFT); 
		robot.keyPress(KeyEvent.VK_CONTROL);
		pressKey(KeyEvent.VK_A);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		sleep(250);
		pressKey(KeyEvent.VK_BACK_SPACE);
	}
	public void input(String string){
		char[] chars = string.toCharArray();
		for (int index = 0; index < chars.length; index++){ 
			int VK = (int)chars[index] + 48;
			pressKey(VK);
		}
	}
	public String chooseText(){ 
		click(QGroupCrawler.MOUSE_LEFT);
		robot.keyPress(KeyEvent.VK_CONTROL);
		pressKey(KeyEvent.VK_A);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		sleep(400);
		robot.keyPress(KeyEvent.VK_CONTROL);
		pressKey(KeyEvent.VK_C);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		sleep(400);
		  
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();   
        Transferable transferable = clipboard.getContents(null);  
        if (transferable != null) {    
            if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {  
                try {  
                    return (String) transferable.getTransferData(DataFlavor.stringFlavor);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }   
		return "";
	}
	public String findQGroupDesc(String qGroupNumber){
		move(1920 + 900, 340);
		clear();
		sleep(500);
		input(qGroupNumber);
		
		move(1920 + 1160, 340);
		sleep(200);
		click(QGroupCrawler.MOUSE_LEFT);
		sleep(5000);

		move(1920 + 620, 450);
		click(QGroupCrawler.MOUSE_LEFT);
		sleep(5000);
		 
		move(1920 + 740, 470);
		String text = chooseText(); 

		move(1920 + 1192, 252);
		click(QGroupCrawler.MOUSE_LEFT);
		return text;
	}
	public static void main(String[] args) {
		QGroupCrawler crawler = new QGroupCrawler();
		
//		String desc = crawler.findQGroupDesc("430148050");
		
		System.out.println(new QGroupCrawler().findQGroupDesc("24479682"));
		System.out.println(new QGroupCrawler().findQGroupDesc("430148050")); 
	} 
}


















